package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.widget.chatrow.EaseCustomChatRowProvider;
import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.StringUtil;

/**
 * Created by william_sun on 2016-08-02.
 * Description:环信聊天会话页面
 */
public class ChatFragmentActivity extends BaseFragmentActivity {

    private LinearLayout ll_title;
    private LinearLayout ll_whole;
    private EaseChatFragment chatFragment;

    private String easeuser;//talking id
    private String easeuserName;

    @Override
    protected void updateByHandler(Message msg) {
        dismisssLoading();
        switch (msg.what) {
            case MessageCode.GET_USERID_SUCCESS:
                if (msg.obj != null){
                    Intent    intent = new Intent(mContext, PatientInfoActivity.class);
                    intent.putExtra("patientId", msg.obj.toString());
                    startActivity(intent);
                }else {
                    toastMsg("获取个人信息失败!");
                }
                break;
            case MessageCode.GET_USERID_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                }else {
                    toastMsg("获取个人信息失败!");
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chatfragment);

        easeuser = getIntent().getStringExtra(EaseConstant.EXTRA_USER_ID);
        easeuserName = getIntent().getStringExtra("name");

//        EMOptions options = new EMOptions();
//        options.setAcceptInvitationAlways(false);
//        EaseUI.getInstance().init(this, null);
        initView();

    }

    private void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_chatfragment_title);
        ll_whole = (LinearLayout) findViewById(R.id.ll_chatfragment);

//        setTitlePadding(ll_title);

        //new出EaseChatFragment或其子类的实例
        chatFragment = new EaseChatFragment(loginInfo.getName(), loginInfo.getHeadImg(), loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient), easeuserName);
//        //传入参数
        Bundle args = new Bundle();
        args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
        args.putString(EaseConstant.EXTRA_USER_ID, easeuser);
        chatFragment.setArguments(args);
        chatFragment.setChatFragmentListener(new EaseChatFragment.EaseChatFragmentHelper() {
            @Override
            public void onSetMessageAttributes(EMMessage message) {

            }

            @Override
            public void onEnterToChatDetails() {

            }

            @Override
            public void onAvatarClick(String username) {
                if (username != null&&easeuser.equals(username)) {
                    String[] split = username.split("ejh");
                    if (split.length==2){
                        loading();
                        YiJiaHuNetWorkUtils.getUserIdByTel(split[1],handler);
                    }
                }
                // Log.e("-->点击",username);
            }

            @Override
            public void onAvatarLongClick(String username) {

            }

            @Override
            public boolean onMessageBubbleClick(EMMessage message) {
                return false;
            }

            @Override
            public void onMessageBubbleLongClick(EMMessage message) {

            }

            @Override
            public boolean onExtendMenuItemClick(int itemId, View view) {
                return false;
            }

            @Override
            public EaseCustomChatRowProvider onSetCustomChatRowProvider() {
                return null;
            }

            @Override
            public void onSendMsg() {
                if (loginInfo.getUserStyle().equals(YjhApplication.userstyle_nurse)) {
                    StringUtil.zhugeTrack(mContext, "指导-发送按钮", "指导信息发送按钮点击数");
                } else if (loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient)) {
                    StringUtil.zhugeTrack(mContext, "咨询中的聊天信息发送按钮", "咨询中发送信息按钮点击数");
                }
                YiJiaHuNetWorkUtils.increaseGuideTimes();
            }
        });
        getSupportFragmentManager().beginTransaction().add(R.id.ll_chatfragment, chatFragment).commit();
    }
}
